package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dto.FollowBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Autowired
    ApBehaviorService apBehaviorService;

    @Autowired
    KafkaTemplate kafkaTemplate;
    /**
     * 保存关注行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //检查数据
        if (dto==null||dto.getArticleId()==null||dto.getFollowId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = ApUserThreadLocalUtils.getUser();
        if (dto.getUserId()==null&&(user==null||user.getId()==null)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getUserId()==null){
            dto.setUserId(user.getId());
        }
        ApBehaviorEntry behaviorEntry = apBehaviorService.getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, dto.getUserId()));
        if (behaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
           /* behaviorEntry=new ApBehaviorEntry();
            behaviorEntry.setCreatedTime(new Date());
            behaviorEntry.setEntryId(dto.getUserId());
            behaviorEntry.setType((short)1);
            apBehaviorService.save(behaviorEntry);*/
        }

        ApFollowBehavior followBehavior = getOne(Wrappers.<ApFollowBehavior>lambdaQuery().eq(ApFollowBehavior::getArticleId, dto.getArticleId()).eq(ApFollowBehavior::getEntryId, behaviorEntry.getId()));

            //保存数据
            if (followBehavior==null){
                followBehavior=new ApFollowBehavior();
                followBehavior.setArticleId(dto.getArticleId());
                followBehavior.setCreatedTime(new Date());
                followBehavior.setEntryId(behaviorEntry.getId());
                followBehavior.setFollowId(dto.getFollowId());
                save(followBehavior);
            }else {
                removeById(followBehavior);
            }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
